package com.x.xiaoshuo.manager;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.view.View;

import com.x.mvp.utils.DensityUtils;
import com.x.xiaoshuo.R;
import com.x.xiaoshuo.XApplication;
import com.x.xiaoshuo.entity.ReadTheme;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class ThemeManager {


    public static final int DRAWABLE_START = 1000;

    public static final int NORMAL = 0;
    public static final int YELLOW = 1;
    public static final int GREEN = 2;
    public static final int LEATHER = 3;
    public static final int GRAY = 4;
    public static final int COLOR_ = 5;
    public static final int NIGHT = 6;
    public static final int KRAFT = DRAWABLE_START + 7;
    public static final int SOFT = DRAWABLE_START + 8;


    public static final HashMap<Integer, Integer> pageBgColor = new HashMap<>();
    public static final HashMap<Integer, Integer> pageTEXTColor = new HashMap<>();

    static {
        pageBgColor.put(NORMAL, Color.parseColor("#fffae6"));
        pageBgColor.put(YELLOW, Color.parseColor("#deefe6"));
        pageBgColor.put(GREEN, Color.parseColor("#f9eddf"));
        pageBgColor.put(LEATHER, Color.parseColor("#e5d7bd"));
        pageBgColor.put(GRAY, Color.parseColor("#354f44"));
        pageBgColor.put(COLOR_, Color.parseColor("#283434"));
        pageBgColor.put(NIGHT, Color.parseColor("#040404"));
        pageBgColor.put(KRAFT, R.drawable.read_kraft);
        pageBgColor.put(SOFT, R.drawable.read_mode_soft_bg);

        pageTEXTColor.put(NORMAL, Color.parseColor("#4a4c4b"));
        pageTEXTColor.put(YELLOW, Color.parseColor("#4a4c4b"));
        pageTEXTColor.put(GREEN, Color.parseColor("#4a4c4b"));
        pageTEXTColor.put(LEATHER, Color.parseColor("#4a4c4b"));
        pageTEXTColor.put(GRAY, Color.parseColor("#9fa391"));
        pageTEXTColor.put(COLOR_, Color.argb(255, 22, 48, 62));
        pageTEXTColor.put(NIGHT, Color.argb(255, 22, 48, 62));
        pageTEXTColor.put(KRAFT, Color.parseColor("#4a4c4b"));
        pageTEXTColor.put(SOFT, Color.parseColor("#4a4c4b"));
    }


    public static void setReaderTheme(int theme, View view) {
        switch (theme) {
            case NORMAL:
                view.setBackgroundResource(R.drawable.theme_white_bg);
                break;
            case YELLOW:
                view.setBackgroundResource(R.drawable.theme_yellow_bg);
                break;
            case GREEN:
                view.setBackgroundResource(R.drawable.theme_green_bg);
                break;
            case LEATHER:
                view.setBackgroundResource(R.drawable.theme_leather_bg);
                break;
            case GRAY:
                view.setBackgroundResource(R.drawable.theme_gray_bg);
                break;
            case NIGHT:
                view.setBackgroundResource(R.drawable.theme_night_bg);
                break;
            default:
                break;
        }
    }

    public static int getThemeTextColor(int theme) {
        return pageTEXTColor.get(theme);
    }

    public static Bitmap getThemeDrawable(int theme) {
        Bitmap bmp = Bitmap.createBitmap(DensityUtils.getScreenWidth(), DensityUtils.getScreenHeight(), Bitmap.Config.ARGB_8888);
        if (theme > DRAWABLE_START) {
            bmp = BitmapFactory.decodeResource(XApplication.getInstance().getResources(), pageBgColor.get(theme));
        } else {
            bmp.eraseColor(pageBgColor.get(theme));
        }

//        switch (theme) {
//            case NORMAL:
//
//                break;
//            case YELLOW:
//                bmp.eraseColor(ContextCompat.getColor(XApplication.getInstance(), R.color.read_theme_yellow));
//                break;
//            case GREEN:
//                bmp.eraseColor(ContextCompat.getColor(XApplication.getInstance(), R.color.read_theme_green));
//                break;
//            case LEATHER:
//                bmp = BitmapFactory.decodeResource(XApplication.getInstance().getResources(), R.drawable.theme_leather_bg);
//                break;
//            case GRAY:
//                bmp.eraseColor(ContextCompat.getColor(XApplication.getInstance(), R.color.read_theme_gray));
//                break;
//            case NIGHT:
//                bmp.eraseColor(ContextCompat.getColor(XApplication.getInstance(), R.color.read_theme_night));
//                break;
//            case COLOR_:
//                bmp.eraseColor(Constant.pageBgColor[COLOR_]);
//                break;
//            case SOFT:
//                break;
//            case KRAFT:
//                break;
//            default:
//                break;
//        }
        return bmp;
    }

    public static List<ReadTheme> getReaderThemeData(int curTheme) {
        int[] themes = {NORMAL, YELLOW, GREEN, LEATHER, GRAY, COLOR_, NIGHT, KRAFT, SOFT};
        List<ReadTheme> list = new ArrayList<>();
        ReadTheme theme;
        for (int i = 0; i < themes.length; i++) {
            theme = new ReadTheme();
            theme.theme = themes[i];
            if (themes[i] > DRAWABLE_START) {
                theme.res = pageBgColor.get(themes[i]);
                theme.type = DRAWABLE_START;
            } else {
                theme.color = pageBgColor.get(themes[i]);
                theme.type = 0;
            }
            theme.textColor = pageTEXTColor.get(themes[i]);
            list.add(theme);
        }
        return list;
    }

}
